<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 字符串辅助函数 扩展
 */

/**
 * 截取字符串,支持字符编码,默认为utf-8
 *
 * @param string $string 要截取的字符串编码
 * @param int $start     开始截取
 * @param int $length    截取的长度
 * @param string $charset 原妈编码,默认为UTF8
 * @param boolean $dot    是否显示省略号,默认为false
 * @return string 截取后的字串
 */
function my_substr($string, $start, $length, $charset = 'utf-8', $dot = false) {
    switch (strtolower($charset)) {
        case 'gbk':
            $string = substr_for_gbk($string, $start, $length, $dot);
            break;
        case 'utf-8':
            $string = substr_for_utf8($string, $start, $length, $dot);
            break;
        default:
            $string = substr($string, $start, $length);
    }
    return $string;
}

/**
 * 求取字符串长度
 *
 * @param string $string  要计算的字符串编码
 * @param string $charset 原始编码,默认为UTF8
 * @return int
 */
function my_strlen($string, $charset = 'utf-8') {
    switch (strtolower($charset)) {
        case 'gbk':
            $count = strlen_for_gbk($string);
            break;
        case 'utf-8':
            $count = strlen_for_utf8($string);
            break;
        default:
            $count = strlen($string);
    }
    return $count;
}

/**
 * 将变量的值转换为字符串
 *
 * @param mixed $input   变量
 * @param string $indent 缩进,默认为''
 * @return string
 */
function var_to_string($input, $indent = '') {
    switch (gettype($input)) {
        case 'string':
            return "'" . str_replace(array("\\", "'"), array("\\\\", "\\'"), $input) . "'";
        case 'array':
            $output = "array(\r\n";
            foreach ($input as $key => $value) {
                $output .= $indent . "\t" . var_to_string($key, $indent . "\t") . ' => ' . var_to_string($value, $indent . "\t");
                $output .= ",\r\n";
            }
            $output .= $indent . ')';
            return $output;
        case 'boolean':
            return $input ? 'true' : 'false';
        case 'NULL':
            return 'NULL';
        case 'integer':
        case 'double':
        case 'float':
            return "'" . (string )$input . "'";
    }
    return 'NULL';
}

/**
 * 将数据用json加密
 *
 * @param mixed $value 需要加密的数据
 * @param string $charset 字符编码
 * @return string 加密后的数据
 */
function my_json_encode($value, $charset = 'utf-8') {
    return json_encode($value);
}

/**
 * 将json格式数据解密
 *
 * @param string $value 待解密的数据
 * @param string $charset 解密后字符串编码
 * @return mixed 解密后的数据
 */
function my_json_decode($value, $charset = 'utf-8') {
    return json_decode($value, true);
}

/**
 * 以utf8格式截取的字符串编码
 *
 * @param string $string  要截取的字符串编码
 * @param int $start      开始截取
 * @param int $length     截取的长度，默认为null，取字符串的全长
 * @param boolean $dot    是否显示省略号，默认为false
 * @return string
 */
function substr_for_utf8($string, $start, $length = null, $dot = false) {
    $l = strlen($string);
    $p = $s = 0;
    if (0 !== $start) {
        while ($start-- && $p < $l) {
            $c = $string[$p];
            if ($c < "\xC0") $p++;
            elseif ($c < "\xE0") $p += 2;
            elseif ($c < "\xF0") $p += 3;
            elseif ($c < "\xF8") $p += 4;
            elseif ($c < "\xFC") $p += 5;
            else  $p += 6;
        }
        $s = $p;
    }

    if (empty($length)) {
        $t = substr($string, $s);
    } else {
        $i = $length;
        while ($i-- && $p < $l) {
            $c = $string[$p];
            if ($c < "\xC0") $p++;
            elseif ($c < "\xE0") $p += 2;
            elseif ($c < "\xF0") $p += 3;
            elseif ($c < "\xF8") $p += 4;
            elseif ($c < "\xFC") $p += 5;
            else  $p += 6;
        }
        $t = substr($string, $s, $p - $s);
    }

    $dot && ($p < $l) && $t .= "...";
    return $t;
}

/**
 * 以gbk格式截取的字符串编码
 *
 * @param string $string  要截取的字符串编码
 * @param int $start      开始截取
 * @param int $length     截取的长度，默认为null，取字符串的全长
 * @param boolean $dot    是否显示省略号，默认为false
 * @return string
 */
function substr_for_gbk($string, $start, $length = null, $dot = false) {
    $l = strlen($string);
    $p = $s = 0;
    if (0 !== $start) {
        while ($start-- && $p < $l) {
            if ($string[$p] > "\x80") $p += 2;
            else  $p++;
        }
        $s = $p;
    }

    if (empty($length)) {
        $t = substr($string, $s);
    } else {
        $i = $length;
        while ($i-- && $p < $l) {
            if ($string[$p] > "\x80") $p += 2;
            else  $p++;
        }
        $t = substr($string, $s, $p - $s);
    }

    $dot && ($p < $l) && $t .= "...";
    return $t;
}

/**
 * 以utf8求取字符串长度
 *
 * @param string $string     要计算的字符串编码
 * @return int
 */
function strlen_for_utf8($string) {
    $l = strlen($string);
    $p = $c = 0;
    while ($p < $l) {
        $a = $string[$p];
        if ($a < "\xC0") $p++;
        elseif ($a < "\xE0") $p += 2;
        elseif ($a < "\xF0") $p += 3;
        elseif ($a < "\xF8") $p += 4;
        elseif ($a < "\xFC") $p += 5;
        else  $p += 6;
        $c++;
    }
    return $c;
}

/**
 * 以gbk求取字符串长度
 *
 * @param string $string     要计算的字符串编码
 * @return int
 */
function strlen_for_gbk($string) {
    $l = strlen($string);
    $p = $c = 0;
    while ($p < $l) {
        if ($string[$p] > "\x80") $p += 2;
        else  $p++;
        $c++;
    }
    return $c;
}

/* End of file MY_string_helper.php */
/* Location: ./helpers/MY_string_helper.php */
